package org.example.struct.array;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/12/5 10:20
 * @apiNote
 */

public class Test00290WordPattern {

    public static void main(String[] args) {
        System.out.println(wordPattern("abba", "dog cat cat dog"));
    }

    public static boolean wordPattern(String pattern, String s) {
        String[] words = s.split(" ");
        if (pattern.length() != words.length) {
            return false;
        }
        Map<Character,String> map1 = new HashMap<>();
        Map<String,Character> map2 = new HashMap<>();
        for(int i = 0; i < pattern.length(); i++){
            char c = pattern.charAt(i);
            if(!map1.containsKey(c)){
                map1.put(c,words[i]);
            }else{
                if(!map1.get(c).equals(words[i])) {
                    return false;
                }
            }
        }
        for(int i = 0; i < words.length; i++){
            String  word = words[i];
            if(!map2.containsKey(word)){
                map2.put(word,pattern.charAt(i));
            }else{
                if(!map2.get(word).equals(pattern.charAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }
}
